<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.titleCn"
          clearable
          placeholder="中文公告标题"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="800px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="120px" inline>
          <el-col :span="24">
            <el-form-item label="发布时间">
              <el-date-picker
                v-model="form.publishTime"
                placeholder="选择日期"
                type="datetime"
                :default-time="form.publishTime"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中文公告标题" prop="titleCn">
              <el-input v-model="form.titleCn" style="width: 220px;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文公告标题" prop="titleEn">
              <el-input v-model="form.titleEn" style="width: 220px;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否置顶">
              <el-radio v-for="item in dict.true_false" :key="item.id" v-model="form.isTop" :label="item.dictKey">
                {{ item.dictValue }}
              </el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio v-for="item in dict.true_false" :key="item.id" v-model="form.status" :label="item.dictKey">
                {{ item.dictValue }}
              </el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中文公告内容" prop="contentCn">
              <el-input v-model="form.contentCn" :rows="10" type="textarea" style="width: 220px;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文公告内容" prop="contentEn">
              <el-input v-model="form.contentEn" :rows="10" type="textarea" style="width: 220px;" />
            </el-form-item>
          </el-col>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        row-key="id"
        stripe
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="titleCn" label="中文公告标题" :show-overflow-tooltip="true"       />
        <el-table-column prop="titleEn" label="英文公告标题" :show-overflow-tooltip="true"       />
        <el-table-column prop="contentCn" label="中文公告内容" :show-overflow-tooltip="true"       />
        <el-table-column prop="contentEn" label="英文公告内容" :show-overflow-tooltip="true"       />
        <el-table-column prop="isTop" label="是否置顶"      >
          <template slot-scope="scope">
            <mall-switch
              :row="scope.row"
              :dict-data="dict.label.true_false"
              :row-data="scope.row.isTop"
              row-filed="isTop"
              to-true-character="置顶"
              to-false-character="取消置顶"
              :title="scope.row.titleCn"
            />
          </template>
        </el-table-column>
        <el-table-column prop="publishTime" label="发布时间"       />
        <el-table-column prop="status" label="状态"      >
          <template slot-scope="scope">
            <mall-switch
              :row="scope.row"
              :dict-data="dict.label.noticeStatus"
              :row-data="scope.row.status"
              row-filed="status"
              :title="scope.row.titleCn"
            />
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','notice:edit','notice:del'])" label="操作" width="150px"      >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudNotice from '@/api/sms/notice'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import mallSwitch from '@/components/mallSwitch'

const defaultForm = {
  id: null,
  titleCn: null,
  titleEn: null,
  contentCn: null,
  contentEn: null,
  isTop: false,
  publishTime: null,
  status: false
}
export default {
  name: 'Notice',
  components: { pagination, crudOperation, rrOperation, udOperation, mallSwitch },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['true_false', 'noticeStatus'],
  cruds() {
    return CRUD({ title: '公告管理', url: 'notice/page', sort: 'id:desc', crudMethod: { ...crudNotice }})
  },
  data() {
    return {
      modelPublishTime: '',
      permission: {
        add: ['admin', 'notice:add'],
        edit: ['admin', 'notice:edit'],
        del: ['admin', 'notice:del']
      },
      rules: {
        titleCn: [
          { required: true, message: '中文公告标题不能为空', trigger: 'blur' }
        ],
        titleEn: [
          { required: true, message: '英文公告标题不能为空', trigger: 'blur' }
        ],
        contentCn: [
          { required: true, message: '中文公告内容不能为空', trigger: 'blur' }
        ],
        contentEn: [
          { required: true, message: '英文公告内容不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'titleCn', display_name: '中文公告标题' },
        { key: 'titleEn', display_name: '英文公告标题' },
        { key: 'isTop', display_name: '是否置顶' },
        { key: 'status', display_name: '状态' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>
</style>
